package com.cckj.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.regex.Pattern;

/**
 * xxx
 *
 * @author chengni
 * @date 2021/8/20 上午10:05
 */
@Slf4j
public class EasyBootUtil {

    /** SecureRandom is preferred to Random */
    private static SecureRandom random = null;

    /**
     * 验证手机号格式
     *
     * @param phone
     * @return
     */
    public static boolean validPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,1,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
        return Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(phone).find();
    }

    /**
     * 生成6位随机数
     * @return 6位随机数
     * @author chengni
     * @date 2020/12/31 16:13
     */
    public static String randomCode() {
        if (random == null){
           try {
               random = SecureRandom.getInstanceStrong();
           } catch (NoSuchAlgorithmException e) {
               log.error("SecureRandom初始化失败", e);
               return String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
           }
        }
        return String.valueOf(random.nextInt(899999) + 100000);
    }
}
